package com.ysyx.ysyx_passengerinfo.controller;

import com.ysyx.ysyx_passengerinfo.dto.BaseDto;
import com.ysyx.ysyx_passengerinfo.pojo.PassengerPojo;
import com.ysyx.ysyx_passengerinfo.qo.PassengerQo;
import com.ysyx.ysyx_passengerinfo.service.OrderPojoService;
import com.ysyx.ysyx_passengerinfo.service.PassengerPojoService;
import com.ysyx.ysyx_passengerinfo.util.Sample;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * Created by IntelliJ IDEA.
 *
 * @description: xxxx接口/类
 * @author:薛宇晖
 * @create:2023/4/6 20:17
 */

@RestController
@RequestMapping("/passenger")
public class PassengerController {


    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private PassengerPojoService passengerPojoService;

    @Autowired
    private OrderPojoService orderPojoService;
    /*
     * @Description: (作用)
     * @params: acc,pwd,code
     * @return:
     * @Author: 薛宇晖
     * @create:2023/4/6
     */
    @RequestMapping("/login/byDatabaseAcc")
    @ResponseBody
    public BaseDto LoginByDatabaseAcc(@RequestBody PassengerQo qo){

        BaseDto baseDto = passengerPojoService.LoginByAcc(qo);

        return baseDto;
    }

    /*
     * @Description: (作用)
     * @params: tel code
     * @return: 101手机号未被注册,帮他注册 102登录成功 103验证码错误
     * @Author: 薛宇晖
     * @create:2023/4/7
     */
    @RequestMapping("/login/byTel")
    @ResponseBody
    public BaseDto LoginByTel(@RequestBody PassengerQo qo){
        BaseDto baseDto = passengerPojoService.LoginByTel(qo);
        return baseDto;
    }


    @RequestMapping("/login/byOpenid")
    public String LoginByOpenid(@RequestBody PassengerQo qo){

        return "";
    }



    @RequestMapping(value = "/login/getCode/{tel}",method = RequestMethod.GET)
    public void getCheckCode(@PathVariable String tel) throws Exception {
        System.err.println("打印"+tel);
//        java.util.List<String> args = java.util.Arrays.asList(args_);
        // 工程代码泄露可能会导致AccessKey泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议使用更安全的 STS 方式，更多鉴权访问方式请参见：https://help.aliyun.com/document_detail/378657.html
        com.aliyun.dysmsapi20170525.Client client = Sample.createClient("LTAI5tEB6gJYarMLG5FaR7Ks", "obHaxsxSOPLg5NexwTkXaQYMPhDBWn");

        //生成四位随机数
        String x;//定义两变量

        Random ne=new Random();//实例化一个random的对象ne

        x= String.valueOf(ne.nextInt(9999-1000+1)+1000);//为变量赋随机值1000-9999

        System.out.println("产生的随机数是:"+x);//输出

        //将生成的随机数存在redis中
        RBucket<Object> bucket = redissonClient.getBucket("login:code:" + tel);
        bucket.set(x);
        bucket.expire(300, TimeUnit.SECONDS);


//        com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest()
//                .setSignName("云上易行")
//                .setTemplateCode("SMS_275410540")
//                .setPhoneNumbers("15060186858")
//                .setTemplateParam("{\"code\":\""+x+"\"}");
//        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
//        try {
//            // 复制代码运行请自行打印 API 的返回值
//            client.sendSmsWithOptions(sendSmsRequest, runtime);
//        } catch (TeaException error) {
//            // 如有需要，请打印 error
//            com.aliyun.teautil.Common.assertAsString(error.message);
//        } catch (Exception _error) {
//            TeaException error = new TeaException(_error.getMessage(), _error);
//            // 如有需要，请打印 error
//            com.aliyun.teautil.Common.assertAsString(error.message);
//        }
    }

    /*
     * @Description: (作用)查询某个乘客
     * @params: Integer passengerId
     * @return: BaseDto(101,"乘客信息查询成功",passengerPojos)
     *          BaseDto(201,"乘客信息不存在",null)
     * @Author: 郭宇轩
     * @create:2023/4/14
     */
    @RequestMapping(value = "/{passengerId}",method = RequestMethod.GET)
    public BaseDto selectPassengerByPassengerId(@PathVariable Integer passengerId){
        BaseDto baseDto = passengerPojoService.selectPassengerByPassengerId(passengerId);
        return baseDto;
    }



    /*
     * @Description: (作用)查询全部乘客
     * @params: Integer passengerId
     * @return: BaseDto(101,"乘客信息查询成功",passengerPojos)
     *          BaseDto(201,"乘客信息不存在",null)
     * @Author: 郭宇轩
     * @create:2023/4/14
     */
    @RequestMapping(value = "/",method = RequestMethod.GET)
    public BaseDto selectAllPassenger(){
        BaseDto baseDto = passengerPojoService.selectAllPassenger();
        return baseDto;
    }

    /*
     * @Description: (作用)乘客查询所有订单
     * @params: Integer passengerId
     * @return:  BaseDto(0,null,list为订单list)
     * @Author: 郭宇轩
     * @create:2023/4/21
     */
    @RequestMapping(value = "/selectAllOrder",method = RequestMethod.GET)
    public BaseDto selectAllOrder(Integer passengerId){
        BaseDto baseDto = passengerPojoService.selectAllOrder(passengerId);
        return baseDto;
    }


    @RequestMapping("/reg")
    public BaseDto passengerReg(@RequestBody PassengerPojo pojo){
        return passengerPojoService.passengerReg(pojo);
    }

    @RequestMapping(value = "/checkCode/{code}/{tel}",method = RequestMethod.GET)
    public BaseDto checkCode(@PathVariable String code,@PathVariable String tel){
        String o = (String) redissonClient.getBucket("login:code:" + tel).get();
        if (o==null){
            return new BaseDto(201,"验证码错误",null);
        }
        if (o.equals(code)){
            return new BaseDto(202,"正确",null);
        }else {
            return new BaseDto(201,"验证码错误",null);
        }

    }


    @RequestMapping("/fixPwd")
    public BaseDto fixPwd(@RequestBody PassengerPojo pojo){
        return passengerPojoService.passengerFixPwd(pojo);
    }


}
